package com.atf.atfconfigurator.views;

import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class WebContainerView extends VerticalLayout implements View{
	private PropertiesConfiguration wasConfigProperty;
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();	
		setSpacing(true);
        setMargin(true);

        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
		final FormLayout form = new FormLayout();
        form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        addComponent(form);
        
       //httpTransportNumber
        final TextField httpTransportNumber = new TextField("HTTP Transport Number");
        httpTransportNumber.setWidth("50%");
        httpTransportNumber.setValue(wasConfigProperty.getString("httpTransportNumber",""));
        httpTransportNumber.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("httpTransportNumber", value);
			}
        	
        });
        form.addComponent(httpTransportNumber);
        
      //httpTransportNumber
        final TextField httpsTransportNumber = new TextField("HTTPS Transport Number");
        httpsTransportNumber.setWidth("50%");
        httpsTransportNumber.setValue(wasConfigProperty.getString("httpsTransportNumber",""));
        httpsTransportNumber.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("httpsTransportNumber", value);
			}
        	
        });
        form.addComponent(httpsTransportNumber);
        
        
      //removeAndAddTransport
        CheckBox removeAndAddTransport = new CheckBox("AddTransport");
        if( wasConfigProperty.getString("removeAndAddTransport", "NO").equalsIgnoreCase("YES")){
        	removeAndAddTransport.setValue(true);
        }
        removeAndAddTransport.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				if( (Boolean) event.getProperty().getValue()){
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("removeAndAddTransport", "YES");
				}else{
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("removeAndAddTransport", "NO");
				}
			}        	
        });
        form.addComponent(removeAndAddTransport);
        
        
        //JSESSIONID
        final TextField jsessionId = new TextField("Alternate JSESSIONID ");
        jsessionId.setWidth("50%");
        jsessionId.setValue(wasConfigProperty.getString("JSESSIONID",""));
        jsessionId.addValueChangeListener(new Property.ValueChangeListener(){

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				String value = (String) event.getProperty().getValue();
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("JSESSIONID", value);
			}
        	
        });
        form.addComponent(jsessionId);
        
	}

}
